import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginInterface from "@/views/LoginInterface";
import Mi from "@/views/Mi";


Vue.use(VueRouter)

const routes = [
    {path: '/login', name: 'Login', component: LoginInterface},
    {path: '/mi', name: 'Mi', component: Mi},
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 代表从哪个路径跳转而来
    // next 是一个函数，表示放行
    //     next()  放行    next('/login')  强制跳转

    if (to.path === '/login') return next()//如果用户要访问登陆界面，那么直接放行
    const tokenStr = window.sessionStorage.getItem('token')// 获取token
    if (!tokenStr) {
        return next('/login')//如果用户没有token令牌，则令用户跳转到登陆界面
    } else if (tokenStr !== "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1MTI1NDQyOTksImV4cCI6MTUxMjYzMDY5OX0.eGrsrvwHm-tPsO9r_pxHIQ5i5L1kX9RX444uwnRGaIM") {
        return next('/login')//如果用户的token令牌不正确，则令用户跳转到登陆界面
    }

    next()//否则，直接放行
})

export default router
